/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recanto.das.pedrinhas.dao;

import java.util.ArrayList;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import recanto.das.pedrinhas.bean.Cliente;
import recanto.das.pedrinhas.bean.Endereco;
import util.NewHibernateUtil;

/**
 *
 * @author Guilherme
 */
public class EnderecoDao {
    private Session session;
    private Endereco endereco;    
    
    public EnderecoDao(){
        this.session = NewHibernateUtil.getSessionFactory().openSession();
    }
    
    public void salvar (Endereco endereco){
        try{
            Transaction transaction = session.beginTransaction();
            this.session.save(endereco);
            transaction.commit();
        }catch(HibernateException ex){
           System.out.print("Erro: "+ex); 
        }
    }
    
    public void alterar(Endereco endereco){
        
        try{
            
            Transaction transaction = session.beginTransaction();
            System.out.println("ID DO ENDERECO = " + endereco.getIdEnd());
            this.session.update(endereco);
            
            transaction.commit();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
        }
    }
    
  
  
    
    public void delatar(){
        try{
            this.session.delete(endereco);
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
        }
    }
    
    public Endereco carregaEnderecoPorId(Cliente c){
        try{
            return (Endereco) session.createCriteria(Endereco.class)
                    .add(Restrictions.eq("idCli", c)).uniqueResult();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
            return null;
        }
    }
    
    public Endereco carregaEnderecoPorId1(Cliente c){
        try{
            return (Endereco) session.createCriteria(Endereco.class)
                    .add(Restrictions.eq("idCli", c)).uniqueResult();
        }catch(HibernateException ex){
            System.out.print("Erro: "+ex); 
            return null;
        }
    }
    
    
    public void fechaSession (){
        try{
            session.close();
        }catch(HibernateException ex){
            System.out.println("Erro: "+ex);
        }
    }
}
